Imports System.Linq
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports SISSAT.BusinessLayer.Managers
Imports SISSAT.BusinessLayer.DomainModels
Imports SISSAT.DataAcessLayer.StatementSources
Imports SISSAT.DataAcessLayer.PersistentStorage

Namespace Services
    Public Class ScheduleService
        Inherits ServiceBase
        Implements IScheduleService

        Public Sub Create(employeeId As Integer, _
                          subjectId As Integer, _
                          roomNumber As String, _
                          scheduleDay As String, _
                          startDate As DateTime, _
                          endDate As DateTime, _
                          startTime As DateTime, _
                          endTime As DateTime, _
                          semester As Semesters, _
                          createdBy As Integer) Implements IScheduleService.Create
            StatementSource = _
                New CreateScheduleStatementSource(employeeId, subjectId, roomNumber, scheduleDay, startDate, endDate, startTime, endTime, semester.ToString(), createdBy)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Sub Delete(scheduleId As Integer) Implements IScheduleService.Delete
            StatementSource = New DeleteScheduleStatementSource(scheduleId)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Sub Edit(entity As Schedule, modifiedBy As Integer) Implements IScheduleService.Edit
            StatementSource = _
                New UpdateScheduleStatementSource(entity.ScheduleId, entity.Employee.EmployeeId, entity.Subject.SubjectId, entity.RoomNumber, entity.ScheduleDay, entity.StartDate, entity.EndDate, entity.StartTime, entity.EndTime, entity.Semester.ToString(), modifiedBy)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Function ReadAll() As IEnumerable(Of Schedule) Implements IScheduleService.ReadAll
            StatementSource = New ReadAllScheduleStatementSource()
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return MapDataToEntity(dataReader)
        End Function

        Public Function ReadAllByInstructorId(instructorId As Integer) As IEnumerable(Of Schedule) Implements IScheduleService.ReadAllByInstructorId
            StatementSource = New ReadAllScheduleByInstructorIdStatementSource(instructorId)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return MapDataToEntity(dataReader)
        End Function

        Public Function ReadAllByInstructorId(instructorId As Integer, schoolYear As String, semester As Semesters) As IEnumerable(Of Schedule) Implements IScheduleService.ReadAllByInstructorId
            StatementSource = New ReadAllScheduleByInstructorIdStatementSource(instructorId, Convert.ToString(semester))
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Dim entities As IEnumerable(Of Schedule) = MapDataToEntity(dataReader)

            Return (From entity In entities
                    Where String.Compare(entity.SchoolYear, schoolYear, True) = 0
                    Select entity).Distinct()
        End Function

        Public Function ReadById(scheduleId As Integer) As Schedule Implements IScheduleService.ReadById
            StatementSource = New ReadByScheduleIdStatementSource(scheduleId)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return MapDataToEntity(dataReader).FirstOrDefault()
        End Function

        Private Function MapDataToEntity(dataReader As SqlDataReader) As IEnumerable(Of Schedule)
            Dim entities As New List(Of Schedule)

            If (dataReader.HasRows) Then
                Dim employeeService As IEmployeeService = New EmployeeService()
                Dim subjectService As ISubjectService = New SubjectService()
                Dim matriculationService As IMatriculationService = New MatriculationService

                While (dataReader.Read())
                    Dim entity As New Schedule()
                    Dim employeeId As Integer = Convert.ToInt32(dataReader("EmployeeId"))
                    Dim subjectId As Integer = Convert.ToInt32(dataReader("SubjectId"))
                    Dim scheduleId As Integer = Convert.ToInt32(dataReader("ScheduleId"))

                    entity.ScheduleId = scheduleId
                    entity.Employee = employeeService.ReadById(employeeId)
                    entity.EndDate = Convert.ToDateTime(dataReader("EndDate"))
                    entity.EndTime = Convert.ToDateTime(dataReader("EndTime"))
                    entity.RoomNumber = Convert.ToString(dataReader("RoomNumber"))
                    entity.ScheduleDay = Convert.ToString(dataReader("ScheduleDay"))
                    entity.StartDate = Convert.ToDateTime(dataReader("StartDate"))
                    entity.StartTime = Convert.ToDateTime(dataReader("StartTime"))
                    entity.StudentCount = matriculationService.ReadAllByScheduleId(scheduleId).Count()
                    entity.Semester = _
                        CType([Enum].Parse(GetType(Semesters), dataReader("Semester").ToString()), Semesters)
                    entity.Subject = subjectService.ReadById(subjectId)

                    entities.Add(entity)
                End While
            End If

            Return entities
        End Function

    End Class
End Namespace